package hotel.controller.room;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.util.List;

import hotel.common.MsgBox;
import hotel.dao.HotelDao;
import hotel.modal.Employee;
import hotel.modal.Room;
import hotel.modal.RoomType;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ChoiceBox;

public class RoomChangeController {
	@FXML
	private Label titleLabel;
	@FXML
	private TextField roomNoField;
	@FXML
	private TextField phoneField;
	@FXML
	private ChoiceBox<String> typeNameCh;
	@FXML
	private ChoiceBox<String> statusCh;
	@FXML
	private ChoiceBox<String> principalCh;
	@FXML
	private ComboBox<String> positionCb;
	@FXML
	private Button btnOk;
	@FXML
	private Button btnCancel;
	
	private List<RoomType> roomTypeList = null;
	private List<Employee> employeeList = null;
	private Room room = null;
	private boolean addMode = false;
	private ObservableList<String> typeNameList;
	private ObservableList<String> employeeNameList;
	private ObservableList<String> positionList;
	
	@FXML
	public void initialize() {
		
		// 初始化数据
		initComponent();
	}
	
	private void initComponent() {
		String choice = null;
		roomTypeList = HotelDao.getAllRoomType();
		typeNameList = FXCollections.observableArrayList();
		typeNameCh.setItems(typeNameList);
		for(RoomType t:roomTypeList) {
			choice  = t.getTypeName();
			if(t.isHourRoom()) choice += "(钟点房)";
			typeNameList.add(choice);
		}
		
		List<String> roomPosition = HotelDao.getAllRoomPosition();
		
		if(roomPosition != null) {
			positionList = FXCollections.observableArrayList(roomPosition);
			positionCb.setItems(positionList);
		}
		
		employeeList = HotelDao.getAllEmployee();
		employeeNameList = FXCollections.observableArrayList();
		principalCh.setItems(employeeNameList);
		for(Employee t:employeeList) {
			choice = t.getName() + "(" + t.getEmployeeId() + ")";
			employeeNameList.add(choice);
		}
	}
	
	public void setInitData(Room room) {
		this.room = room;
		if(room != null) {
			room.setModified(false);
			if(!addMode) {
				roomNoField.setText(room.getRoomNo());
				phoneField.setText(room.getPhoneNum());
				positionCb.setValue(room.getPosition());
				typeNameCh.getSelectionModel().select(room.getTypeName());
				statusCh.getSelectionModel().select(room.getStatus() - 1);
				principalCh.getSelectionModel().select(getPrincipalName(room.getPrincipal()) + "(" +room.getPrincipal() +")");
			}else {
				roomNoField.setText(room.getRoomNo());
				statusCh.getSelectionModel().select("可供");
				if(employeeNameList != null && employeeNameList.size() > 0) principalCh.getSelectionModel().select(0);
				if(typeNameList != null && typeNameList.size() > 0) typeNameCh.getSelectionModel().select(0);
				if(positionList != null && positionList.size() > 0) positionCb.getSelectionModel().select(0);
				titleLabel.setText("房间信息添加");
			}
		}
	}
	
	public String getPrincipalName(int id) {
		if(employeeList == null) return "";
		for(int i = 0; i < employeeList.size(); i++){
			if(employeeList.get(i).getEmployeeId() == id) 
				return employeeList.get(i).getName();
		}
		return "";
	}
	
	public void setAddMode() {
		this.addMode = true;
	}
	
	// Event Listener on Button[#btnOk].onAction
	@FXML
	public void onBtnOkAction(ActionEvent event) {
		boolean check = true;
		String roomNo = roomNoField.getText();
		String phone = phoneField.getText();
		String position = positionCb.getValue();
		int roomTypeIndex = typeNameCh.getSelectionModel().getSelectedIndex();
		int principalIndex = principalCh.getSelectionModel().getSelectedIndex();
		// 房间状态代码：1-可供	2-预定	3-入住	4-脏房	5-停用
		int status = statusCh.getSelectionModel().getSelectedIndex() + 1;
		
		if(check && roomNo.length() > 10) {
			check = false;
			MsgBox.showErrorDialog("错误", null, "房间名称过长！");
			roomNoField.requestFocus();
		}
		
		if(principalIndex == -1) principalIndex = 0;
		
		int principal = employeeList.get(principalIndex).getEmployeeId();
		RoomType rt = roomTypeList.get(roomTypeIndex);
		
		if(check) {
			if(phone == null) phone="";
			room.setRoomNo(roomNo);
			room.setPosition(position);
			room.setStatus(status);
			room.setPrincipal(principal);
			room.setTypeId(rt.getTypeId());
			if(addMode) {
				room.setTypeName(rt.getTypeName());
				room.setBedNum(rt.getBedNum());
				room.setDeposit(rt.getDeposit());
				room.setPrice(rt.getPrice());
				room.setHourRoom(rt.isHourRoom());
			}
			room.setPhoneNum(phone);
			room.setModified(true);
			closeWindow();
		}
		check = true;
	}
	// Event Listener on Button[#btnCancel].onAction
	@FXML
	public void onBtnCancelAction(ActionEvent event) {
		room.setModified(false);
		closeWindow();
	}
	
	private void closeWindow() {
		Stage stage = (Stage)btnCancel.getScene().getWindow();
		stage.close();
	}
}
